#include "nasm/symbol/symbol.h"
#include "nasm/bitTools/bitTools.h"

namespace dspnasm {
    std::shared_ptr<Symbol> Symbol::getEqu(const std::string &symbolName, const std::string &segmentName, u32 data) {
        std::shared_ptr<Symbol> symbol = std::make_shared<Symbol>();
        symbol->m_symbolName = symbolName;
        symbol->m_segmentName = segmentName;
        symbol->m_data = std::move(splitI32IntoByte(data,4));
        symbol->m_isEqu = true;
        symbol->m_isGlobal = false;
        symbol->m_isExterned = false;
        return symbol;
    }

    std::shared_ptr<Symbol> Symbol::getGlobal(const std::string &symbolName, const std::string &segmentName) {
        std::shared_ptr<Symbol> symbol = std::make_shared<Symbol>();
        symbol->m_symbolName = symbolName;
        symbol->m_segmentName = segmentName;
        symbol->m_isEqu = false;
        symbol->m_isGlobal = true;
        symbol->m_isExterned = false;
        return symbol;
    }

    std::shared_ptr<Symbol> Symbol::getExterned(const std::string &symbolName, const std::string &segmentName) {
        std::shared_ptr<Symbol> symbol = std::make_shared<Symbol>();
        symbol->m_symbolName = symbolName;
        symbol->m_segmentName = segmentName;
        symbol->m_isExterned = true;
        return symbol;
    }

    std::shared_ptr<Symbol>
    Symbol::getLabel(const std::string &symbolName, const std::string &segmentName, u32 address) {
        std::shared_ptr<Symbol> symbol = std::make_shared<Symbol>();
        symbol->m_symbolName = symbolName;
        symbol->m_segmentName = segmentName;
        symbol->m_isEqu = false;
        symbol->m_isGlobal = false;
        symbol->m_isExterned = false;
        symbol->m_offset = address;
        return symbol;
    }

    std::string Symbol::toString() const {
        std::string str;
        if (m_isEqu) {
            str = "Var : " + m_symbolName + "\tSegment : " + m_segmentName + "\tdata:" + dec2hex(vectorToU32(m_data));
        } else if (m_isExterned) {
            str = "Externed : " + m_symbolName + "\tSegment : " + m_segmentName;
        } else if (m_isGlobal) {
            str = "Global : " + m_symbolName + "\tSegment : " + m_segmentName;
        } else {
            str = " Label : " + m_symbolName + "\tSegment : " + m_segmentName + "\toffset : " + dec2hex(m_offset);
        }
        return str;
    }
}
